package com.banciyuan.bcywebview.biz.main.message.messagenewest;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.TopPost;

/**
 * Created by hy on 2015/7/13.
 */
public class NewestMessageAdapter extends BaseAdapter {
    private Context context;
    private List<TopPost> list;
    private ImageLoader imageLoader;
    private RequestQueue mQueue;
    private ListView mListView;
    private int headerViewCount;

    public NewestMessageAdapter(Context context, List<TopPost> list) {
        this.context = context;
        this.list = list;
        imageLoader = ImageLoader.getInstance();
        mQueue = VolleyQueue.getRquest(context);
    }

    public void setListView(ListView mListView) {
        this.mListView = mListView;
        headerViewCount = mListView.getHeaderViewsCount();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        TopPost item = list.get(position);
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.new_post_list_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ((View) (viewHolder.time.getParent())).setVisibility(View.GONE);
        viewHolder.title.setVisibility(View.GONE);
        if (item.getMulti().size() == 0) {
            viewHolder.imagePics.setVisibility(View.GONE);
        } else {
            viewHolder.imagePics.setVisibility(View.VISIBLE);
            for (int i = 0; i < viewHolder.content.length; i++) {
                if (i < item.getMulti().size()) {
                    viewHolder.content[i].setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(item.getMulti().get(i).getPath(), viewHolder.content[i], ImageOptions.options);
                } else {
                    viewHolder.content[i].setVisibility(View.INVISIBLE);
                }
            }
        }
        String pic_num = item.getPic_num();
        if (!TextUtils.isEmpty(pic_num)) {
            if (Integer.parseInt(pic_num) > 3) {
                viewHolder.pagenum.setVisibility(View.VISIBLE);
            } else {
                viewHolder.pagenum.setVisibility(View.GONE);
            }
        } else {
            viewHolder.pagenum.setVisibility(View.GONE);
        }
        if (item.getUser() != null) {
            if (!TextUtils.isEmpty(item.getUser().getUname())) {
                viewHolder.name.setText(item.getUser().getUname());
            }
            imageLoader.displayImage(item.getUser().getAvatar(), viewHolder.head, ImageOptions.optionHead);
        }
        if (StringUtil.notNullEqual(item.getHave_ding(), "true")) {
            viewHolder.likeNumImg.setImageResource(R.drawable.like_pink);
            viewHolder.likeNum.setText(item.getDing_num());
        } else {
            viewHolder.likeNumImg.setImageResource(R.drawable.like_grey);
            viewHolder.likeNum.setText(item.getDing_num());
            if (item.getDing_num().equals("0")) {
                viewHolder.likeNum.setText(context.getString(R.string.good));
            }
        }
        if (StringUtil.notNullEqual(item.getHave_tuijian(), "true")) {
            viewHolder.recommend.setText(context.getString(R.string.all_recommended));
        } else {
            viewHolder.recommend.setText(context.getString(R.string.all_recommend));
        }
        if (!TextUtils.isEmpty(item.getReply_count()) && !"0".equals(item.getReply_count())) {
            viewHolder.commentNum.setText(item.getReply_count());
        } else {
            viewHolder.commentNum.setText(context.getString(R.string.comment));
        }

        viewHolder.time.setText(StringUtil.getOppositeTime(item.getCtime()));
        viewHolder.intro.setText(Html.fromHtml(item.getPlain()));

        OnClick dailyOnClick = new OnClick(position);
        viewHolder.head.setOnClickListener(dailyOnClick);
        viewHolder.name.setOnClickListener(dailyOnClick);
        viewHolder.intro.setOnClickListener(dailyOnClick);
        viewHolder.recommendLayout.setOnClickListener(dailyOnClick);
        viewHolder.likeLayout.setOnClickListener(dailyOnClick);
        viewHolder.commentLayout.setOnClickListener(dailyOnClick);
        viewHolder.imagePics.setOnClickListener(dailyOnClick);
        return convertView;
    }

    class OnClick implements View.OnClickListener {

        int position;
        TopPost mItem;

        public OnClick(int position) {
            this.position = position;
            if (position < list.size()) {
                mItem = (TopPost) getItem(position);
            }
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ground_name_tv:
                case R.id.ground_head_img:
                    if (null != mItem) {
                        gotoUtil.gotoAct(context, SmoothPersonActivity.class, mItem.getUid());
                    }
                    break;
                case R.id.ground_comment_rly:
                    DetailDataHelper.gotoGroupDetail(context, mItem.getGid(), mItem.getPost_id(), true);
                    break;
                case R.id.ground_like_rly:
                    doLike(position);
                    break;
                case R.id.ground_recommend_rly:
                    doRecommend(position);
                    break;
                case R.id.daily_intro_tv:
                case R.id.rl_daily_pics:
                    DetailDataHelper.gotoGroupDetail(context, mItem.getGid(), mItem.getPost_id(), false);
                    break;
                default:
                    break;
            }
        }
    }

    public void updateView(int itemIndex, ListView mListView, TopPost item) {
        if (mListView == null) return;
        View convertView = null;
        try {
            convertView = mListView.getChildAt(itemIndex - mListView.getFirstVisiblePosition());
        } catch (Exception e) {
            return;
        }
        if (convertView == null) return;

        ViewHolder mViewHolder = null;
        if (convertView.getTag() instanceof ViewHolder)
            mViewHolder = (ViewHolder) convertView.getTag();
        else return;
        if (mViewHolder == null) return;
        if (StringUtil.notNullEqual(item.getHave_ding(), "true")) {
            mViewHolder.likeNumImg.setImageResource(R.drawable.like_pink);
            mViewHolder.likeNum.setText(item.getDing_num());
        } else {
            mViewHolder.likeNumImg.setImageResource(R.drawable.like_grey);
            mViewHolder.likeNum.setText(item.getDing_num());
            if (item.getDing_num().equals("0")) {
                mViewHolder.likeNum.setText(context.getString(R.string.good));
            }
        }

        if (StringUtil.notNullEqual(item.getHave_tuijian(), "true")) {
            mViewHolder.recommend.setText(context.getString(R.string.all_recommended));
        } else {
            mViewHolder.recommend.setText(context.getString(R.string.all_recommend));
        }
    }

    public void doRecommend(final int postion) {
        TopPost topPost = list.get(postion);
        TimelineLikeHelper.doRecommendGroup(context, mQueue, topPost.getPost_id(), topPost.getHave_tuijian(), new TimelineLikeHelper.TimelineSuccess() {
            @Override
            public void onTimelineSuccess() {
                list.get(postion).setHave_tuijian(String.valueOf(!Boolean.valueOf(list.get(postion).getHave_tuijian())));
                updateView(postion + headerViewCount, mListView, list.get(postion));
            }
        });
    }

    public void doLike(final int postion) {
        TopPost topPost = list.get(postion);
        TimelineLikeHelper.doLikeGroup(context, mQueue, topPost.getPost_id(), topPost.getHave_ding(), new TimelineLikeHelper.TimelineSuccess() {
            @Override
            public void onTimelineSuccess() {
                int count = Integer.parseInt(list.get(postion).getDing_num());
                if (list.get(postion).getHave_ding().equals("false")) {
                    list.get(postion).setHave_ding("true");
                    list.get(postion).setDing_num(String.valueOf(++count));
                } else {
                    list.get(postion).setHave_ding("false");
                    list.get(postion).setDing_num(String.valueOf(--count));
                }
                updateView(postion + headerViewCount, mListView, list.get(postion));
            }
        });
    }

    private class ViewHolder {
        ImageView[] content;
        TextView intro;
        TextView title;
        TextView pagenum;
        RelativeLayout imagePics;
        TextView name;
        ImageView head;
        TextView time;
        TextView likeNum;
        ImageView likeNumImg;
        RelativeLayout likeLayout;
        TextView recommend;
        RelativeLayout recommendLayout;
        TextView commentNum;
        RelativeLayout commentLayout;
        LinearLayout teamNameLayout;

        public ViewHolder(View convertView) {
            head = (ImageView) convertView.findViewById(R.id.ground_head_img);
            content = new ImageView[]{
                    (ImageView) convertView.findViewById(R.id.daily_content_img_one),
                    (ImageView) convertView.findViewById(R.id.daily_content_img_two),
                    (ImageView) convertView.findViewById(R.id.daily_content_img_three)
            };
            name = (TextView) convertView.findViewById(R.id.ground_name_tv);
            recommend = (TextView) convertView.findViewById(R.id.ground_recommend_tv);
            recommendLayout = (RelativeLayout) convertView.findViewById(R.id.ground_recommend_rly);
            time = (TextView) convertView.findViewById(R.id.ground_time_tv);
            likeNum = (TextView) convertView.findViewById(R.id.ground_likenumber_tv);
            likeNumImg = (ImageView) convertView.findViewById(R.id.ground_like_img);
            commentNum = (TextView) convertView.findViewById(R.id.ground_commentnumber_tv);
            intro = (TextView) convertView.findViewById(R.id.daily_intro_tv);
            likeLayout = (RelativeLayout) convertView.findViewById(R.id.ground_like_rly);
            commentLayout = (RelativeLayout) convertView.findViewById(R.id.ground_comment_rly);
            pagenum = (TextView) convertView.findViewById(R.id.daily_pagenum_tv);
            imagePics = (RelativeLayout) convertView.findViewById(R.id.rl_daily_pics);
            teamNameLayout = (LinearLayout) convertView.findViewById(R.id.daily_teamname_rly);
            title = (TextView) convertView.findViewById(R.id.daily_teamname_tv);
        }
    }
}
